package com.yangjiayu.Controller;

import com.yangjiayu.enums.SSEMsgType;
import com.yangjiayu.service.ChatService;
import com.yangjiayu.utils.SSEServer;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.content.Media;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import reactor.core.publisher.Flux;

import java.io.IOException;

/**
 * @Classname sseController
 * @Description TODO
 * @Date 2025/8/12 19:57
 * @Created by YangJiaYu
 */
@Slf4j
@RestController
@RequestMapping("sse")
public class sseController {

    /*
    SSE 会发一个长连接，OPTIONS 预检可能不走 WebMvcConfigurer
Spring 的 SseEmitter 有时候跨域需要在 Controller 方法里单独加响应头 才保险，
因为 CORS 处理器在长连接场景下并不一定完整生效。
可以这样：
     */
    @GetMapping(path = "connect", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public SseEmitter connect(@RequestParam String userId,HttpServletResponse response) throws IOException {
        response.setHeader("Access-Control-Allow-Origin", "http://127.0.0.1:5501");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Connection", "keep-alive");

        log.info("连接到Controller了，用户ID: {}", userId);

        // ✅ 使用你写好的工具类，自动保存 emitter
        SseEmitter emitter = SSEServer.connect(userId);

        // ✅ 发送欢迎消息
        emitter.send(SseEmitter.event()
            .name("open")
            .data("连接成功，欢迎用户：" + userId));
        return emitter;
    }

    /**
     * SSE发送单个消息
     * @param userId
     * @param message
     * @return
     */
    @GetMapping("sendMessage")
    public Object sendMessage(@RequestParam String userId,@RequestParam String message){
        log.info("收到发送消息请求，userId: {}, message: {}", userId, message);
        SSEServer.sendMsg(userId,message, SSEMsgType.MESSAGE);
        return "OK";
    }

    /**
     * SSE发送单个消息 - add
     * @param userId
     * @param message
     * @return
     */
    @GetMapping("sendMessageAdd")
    public Object sendMessageAdd(@RequestParam String userId,@RequestParam String message) throws Exception{
        log.info("收到发送消息请求，userId: {}, message: {}", userId, message);
        for (int i = 0; i < 10; i++) {
            Thread.sleep(200); //模拟流式
            SSEServer.sendMsg(userId,message, SSEMsgType.ADD);
        }
//        SSEServer.sendMsg(userId,message, SSEMsgType.ADD);
        return "OK";
    }



    /**
     * SSE发送群消息
     * @param message
     * @return
     */
    @GetMapping("sendMessageAll")
    public Object sendMessage(@RequestParam String message){
        log.info("收到群发消息请求，message: {}", message);
//        SSEServer.sendMsg(message);
        SSEServer.sendMsgToAllUsers(message);
        return "OK";
    }
}




















